/**
 * JimBot - Java IM Bot
 * Copyright (C) 2006-2009 JimBot project
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ru.jimbot.modules.http;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.log.Logger;


import ru.jimbot.util.Log;
import ru.jimbot.util.MainProps;

/**
 * HTTP-сервер бота. Запускаем Jetty 9.
 * @author Prolubnikov Dmitry
 * @author alex_xpert
 */
public class HTTPServer {
    static Server server;
    
    public static void startServer(String args[]) throws Exception {
        org.eclipse.jetty.util.log.Log.setLog(new NoLogging());
        ServletContextHandler context = new ServletContextHandler();
        for (int i = 0; i < args.length; i += 2) {
            String path = args[i];
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            if (path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }
            context.addServlet(args[i + 1], path);
        }
        server = new Server(MainProps.getIntProperty("http.port"));
        server.setHandler(context);
        Log.info("Запуск HTTP-сервера Jetty...");
        server.start();
        /*JOptionPane.showMessageDialog(null, "HTTP сервер запущен.", "Сообщение",
                                      JOptionPane.INFORMATION_MESSAGE);*/
    }

    public static void stopServer() {
        try {
            System.out.println("Остановка HTTP-сервера Jetty...");
            server.stop();
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
    
    public static class NoLogging implements Logger {
        @Override public String getName() { return "no"; }
        @Override public void warn(String msg, Object... args) { }
        @Override public void warn(Throwable thrown) { }
        @Override public void warn(String msg, Throwable thrown) { }
        @Override public void info(String msg, Object... args) { }
        @Override public void info(Throwable thrown) { }
        @Override public void info(String msg, Throwable thrown) { }
        @Override public boolean isDebugEnabled() { return false; }
        @Override public void setDebugEnabled(boolean enabled) { }
        @Override public void debug(String msg, Object... args) { }
        @Override public void debug(Throwable thrown) { }
        @Override public void debug(String msg, Throwable thrown) { }
        @Override public Logger getLogger(String name) { return this; }
        @Override public void ignore(Throwable ignored) { }
    }
}
